#!/bin/bash

. url.sh || { echo "url脚本打开失败"; exit 1; }
name="03-Jellyfin-8096"
echo "jellyfin页面美化安装中..."
docker exec -it  $name rm -rf /jellyfin/jellyfin-web/jellyfin-crx/
docker exec -it  $name mkdir -p /jellyfin/jellyfin-web/jellyfin-crx/
wget -q --no-check-certificate $urlBash/jellyfincrx/static/css/style.css -O style.css  
wget -q --no-check-certificate $urlBash/jellyfincrx/static/js/common-utils.js -O common-utils.js  
wget -q --no-check-certificate $urlBash/jellyfincrx/static/js/jquery-3.6.0.min.js -O jquery-3.6.0.min.js  
wget -q --no-check-certificate $urlBash/jellyfincrx/static/js/md5.min.js -O md5.min.js  
wget -q --no-check-certificate $urlBash/jellyfincrx/content/main.js -O main.js
docker cp style.css $name:/jellyfin/jellyfin-web/jellyfin-crx/
docker cp common-utils.js $name:/jellyfin/jellyfin-web/jellyfin-crx/
docker cp jquery-3.6.0.min.js $name:/jellyfin/jellyfin-web/jellyfin-crx/
docker cp md5.min.js $name:/jellyfin/jellyfin-web/jellyfin-crx/
docker cp main.js $name:/jellyfin/jellyfin-web/jellyfin-crx/
function Installing() {
	content=$(cat index.html)

	code='<link rel="stylesheet" id="theme-css" href="./jellyfin-crx/style.css" type="text/css" media="all" />\n<script src="./jellyfin-crx/common-utils.js"></script>\n<script src="./jellyfin-crx/jquery-3.6.0.min.js"></script>\n<script src="./jellyfin-crx/md5.min.js"></script>\n<script src="./jellyfin-crx/main.js"></script>'

	new_content=$(echo -e "${content/<\/head>/$code<\/head>}")

	echo -e "$new_content" > index.html
	docker cp ./index.html $name:/jellyfin/jellyfin-web/
}
rm index.html
docker cp $name:/jellyfin/jellyfin-web/index.html ./
if grep -q "jellyfin-crx" index.html; then
    docker cp $name:/jellyfin/jellyfin-web/bak/index.html ./
    Installing
    echo "成功！Index.html 已重新修改！"
else
    docker cp $name:/jellyfin/jellyfin-web/index.html ./
    docker exec -it  $name mkdir -p /jellyfin/jellyfin-web/bak/
    docker cp ./index.html $name:/jellyfin/jellyfin-web/bak/
    Installing
    echo "成功！Index.html 首次安装！"
fi